package com.handpay.zztong.hp.log;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

public class SystemUtility
{
    public static Intent createDialIntent(String number)
    {
        Uri uri = Uri.parse("tel:" + number);
        return new Intent(Intent.ACTION_DIAL, uri);
    }

    public static Intent createOpenWebIntent(String url)
    {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        return intent;
    }

    public static Intent createLocationIntent(double lat, double lon)
    {
        Uri uri = Uri.parse("geo:" + String.valueOf(lat) + "," + String.valueOf(lon) + "");
        return new Intent(Intent.ACTION_VIEW, uri);
    }

    public static Intent createSendMessageIntent(String text)
    {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra("sms_body", text);
        return intent;
    }

    public static Intent createSendMessageIntent(String number, String text)
    {
        Uri uri = Uri.parse("smsto:" + number);
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", text);
        return intent;
    }

    public static Intent createSendMessageIntent(ArrayList<String> numbers, String text)
    {
        String separator = "; ";
        if(android.os.Build.MANUFACTURER.equalsIgnoreCase("samsung"))
        {
            separator = ", ";
        }
        String phoneNumbers = StringUtility.join(separator, numbers);
        Uri uri = Uri.parse("smsto:" + phoneNumbers.toString());
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", text);
        return intent;
    }

    public static Intent createSendEmailIntent(String email)
    {
        Uri uri = Uri.parse("mailto:" + email);
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        return intent;
    }


    public static Intent createSendEmailIntent(String email, String title)
    {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
        intent.putExtra(Intent.EXTRA_SUBJECT, title);
        return intent;
    }

    public static Intent createSendEmailIntentWithContent(String title, String content)
    {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_SUBJECT, title);
        intent.putExtra(Intent.EXTRA_TEXT, content);
        return intent;
    }

    public static Intent createSendEmailIntentNoRFC822(String email, String title)
    {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
        intent.putExtra(Intent.EXTRA_SUBJECT, title);
        return intent;
    }

    public static Intent createSendEmailIntent(String email, String title, String filePath)
    {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
        intent.putExtra(Intent.EXTRA_SUBJECT, title);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));
        return intent;
    }

    public static String getAppPath(Context context)
    {
        PackageManager m = context.getPackageManager();
        String s = context.getPackageName();
        try
        {
            PackageInfo p = m.getPackageInfo(s, 0);
            s = p.applicationInfo.dataDir;
        }
        catch (NameNotFoundException e)
        {
            HPLog.e("PlanetConfiguration", "Error Package name not found ", e);
        }
        return s;
    }
}
